﻿using System;
using System.Windows.Forms;
using Pharmacy.Data;
using Pharmacy.Entities;
using Pharmacy.Windows.Forms.Main;

namespace Pharmacy.Windows.Forms
{
    public partial class FormVoid : DevExpress.XtraEditors.XtraForm
    {
        public Prescription PreviewDN;

        public FormVoid()
        {
            InitializeComponent();
            //btn_Refresh_Click(null, null);

        }

        private void btn_Search_Click(object sender, System.EventArgs e)
        {
            string WhereExpression = "";
            if (tb_FromDate.Text != "" && tb_ToDate.Text != "")
                WhereExpression = "CreateDate Between N'" + tb_FromDate.DateTime.ToString("yyyy-MM-dd 00:00:00") + "' And N'" + tb_ToDate.DateTime.ToString("yyyy-MM-dd 23:59:59") + "'";
            if (tb_PatientCode.Text.Length == 6)
                if (WhereExpression.Length > 0)
                    WhereExpression += "And PatientCode=N'" + tb_PatientCode.Text + "'";
                else
                    WhereExpression = "PatientCode=N'" + tb_PatientCode.Text + "'";
            else if (tb_PatientName.Text.Length > 0)
            {
                if (WhereExpression.Length > 0)
                    WhereExpression += "And PatientName LIKE N'%" + tb_PatientName.Text + "%'";
                else
                    WhereExpression = "PatientName LIKE N'%" + tb_PatientName.Text + "%'";
            }
            else if (tb_DONumber.Text.Length > 0)
            {
                if (WhereExpression.Length > 0)
                    WhereExpression += "And PrescriptionId LIKE N'" + tb_PatientName.Text + "%'";
                else
                    WhereExpression = "PrescriptionId LIKE N'" + tb_PatientName.Text + "%'";
            }
            int count = 0;
            gridControl1.DataSource = DataRepository.PrescriptionProvider.GetPaged(WhereExpression, "CreateDate", 0, Int32.MaxValue,
                                                                                   out count);
        }

        private void btnPreview_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var prescription = (Prescription)gridView1.GetRow(gridView1.FocusedRowHandle);
            DataRepository.PrescriptionProvider.DeepLoad(prescription, true, DeepLoadType.IncludeChildren, typeof(TList<PrescriptionDetail>));
            PreviewDN = prescription;
            FormPrescriptionPreview objPreview = new FormPrescriptionPreview();
            objPreview.LoadReport(prescription, false, "");
            objPreview.Show();
            //Utilities.PreviewDeliveryNote(prescription, reportViewer2);
        }

        private void btn_Clear_Click(object sender, EventArgs e)
        {
            tb_FromDate.Text = tb_ToDate.Text = tb_DONumber.Text = tb_PatientName.Text = tb_PatientCode.Text = "";
        }

        private void btn_Void_Click(object sender, EventArgs e)
        {
            if (cbo_VoidReason.SelectedIndex == -1)
            {
                MessageBox.Show("Please choose void reason");
                cbo_VoidReason.Focus();
            }
            //else if (PreviewDN == null)
            //{
            //    MessageBox.Show("Please preview DN before void");
            //}
            else
            {
                var prescription = (Prescription)gridView1.GetRow(gridView1.FocusedRowHandle);
                if (prescription.IsVoid)
                {
                    MessageBox.Show("This DN already void");
                }
                else
                {
                    DataRepository.PrescriptionProvider.DeepLoad(prescription, true, DeepLoadType.IncludeChildren,
                                                                 typeof(TList<PrescriptionDetail>));

                    if (
                        MessageBox.Show("Are you sure to void DN " + prescription.PrescriptionId, "Void DN",
                                        MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
                    {
                        if (cbo_VoidReason.SelectedIndex == 0)
                            Utilities.CreateDBF(prescription);
                        else
                        {
                            TransactionManager objTransactionManager = DataRepository.Provider.CreateTransaction();
                            objTransactionManager.BeginTransaction();
                            if (Utilities.VoidPrescription(prescription, objTransactionManager, cbo_VoidReason.SelectedItem.ToString(), tb_VoidRemark.Text))
                            {
                                FormPrescriptionPreview objPreview = new FormPrescriptionPreview();
                                string voidReason = "Void Reason:" + cbo_VoidReason.SelectedItem.ToString() + " - " +
                                                    tb_VoidRemark.Text;
                                objPreview.LoadReport(prescription, true, voidReason);
                                objPreview.btn_Print_Click(sender, e);
                                objTransactionManager.Commit();
                                MessageBox.Show("Void success");
                                //var formPrescription = new MiniFormPrescription { MdiParent = Program.MainForm };
                                //formPrescription.TbPatientCode.Text = prescription.PatientCode;
                                //formPrescription.btn_Search_Click(sender,e);
                                //formPrescription.Show();
                            }
                            else
                                objTransactionManager.Rollback();
                        }
                    }
                }
            }
        }
    }
}